package com.talkyun.openx.server;

import com.talkyun.openx.RestfulServiceFactory;
import com.talkyun.openx.plugin.echo.Echo;
import com.talkyun.openx.plugin.echo.User;
import com.talkyun.utils.json.JSON;
import com.talkyun.utils.json.JSONObject;
import org.junit.Assert;

public class EchoTestCase extends AbstractServletTestCase {
    private static RestfulServiceFactory rsf;

    static {
        String url = "http://127.0.0.1:9099/";
        rsf = new RestfulServiceFactory(url);
    }

    private Echo echo = rsf.getService("/openx/echo", Echo.class);

    public void testMatch(){
        System.out.println("/home/ssd".startsWith("/"));
    }

    public void testEcho() {
        logger.info("Testing ...");
        logger.info("Echo service " + echo);

        logger.info(echo.helloByString("kevin"));
        logger.info(echo.hello());

        logger.info("" + echo.helloByPojo(1L, new User("kevin", "123")));

    }

    public void testEchoException() {
        try {
            logger.info(echo.helloByString("exception"));
        } catch (RuntimeException re) {
            JSONObject json = JSON.parseObject(re.getMessage());
            Assert.assertEquals("00000000", json.getString("code"));
        }

        try {
            logger.info(echo.helloByString("AppException"));
        } catch (RuntimeException re) {
            JSONObject json = JSON.parseObject(re.getMessage());
            Assert.assertEquals("10001000", json.getString("code"));
        }
    }
}